/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var averageOfLevels = function(root) {
    const res=[],queue=[root]
          if(!root)return res
          while(queue.length){
              let len=queue.length,sum=0
              for(let i=1;i<=len;i++){
                  let node=queue.shift()
                   sum+=node.val
                  node.left&&queue.push(node.left)
                  node.right&&queue.push(node.right)
              }
              res.push(sum/len)
          }
             return res
   };